package com.wink_172.library.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.InputType;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatEditText;

import com.wink_172.library.utils.DisplayUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 验证码输入框  密码输入框
 * 请自行修改样式
 * */
public class PasswordEditText extends AppCompatEditText {


    public Paint sidePaint;//边框
    public Paint backPaint, textPaint;
    public Context mContext;
    public int spzceX, spzceY;
    public int SingleWidthHeight;//正方形宽高  请注意这个大正方形已经包含了小格子+间距
    public int InputEmpty;//已输入 未输入
    public int backColor, textColor;
    public String mText;
    public int inputNum;//字符数量
    public List<RectF> rectFS;


    public void setCallback(ICallback callback) {
        this.mCallback = callback;
    }

    public ICallback mCallback;

    public interface ICallback {
        void onSendEvent(int event, String content);
    }


    public PasswordEditText(Context context) {
        super(context);
        mContext = context;
        init();
    }

    public PasswordEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    public PasswordEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();
    }


    public void clearText() {
        setText("");
        setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL);
    }

    private void init() {
        setTextColor(0X00ffffff);
        setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL);
        spzceX = DisplayUtil.dp2px(getContext(),4);
        spzceY = DisplayUtil.dp2px(getContext(),4);
        SingleWidthHeight = DisplayUtil.dp2px(getContext(),40);
        InputEmpty = 0xffd0d0d0;
        backColor = 0xfff1f1f1;
        textColor = 0xFF424242;
        sidePaint = new Paint();
        backPaint = new Paint();
        textPaint = new Paint();
        rectFS = new ArrayList<>();

        mText = "";
        inputNum = 6;

        setBackgroundDrawable(null);
        setLongClickable(false);
        setTextIsSelectable(false);
        setCursorVisible(false);

    }

    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        super.onTextChanged(text, start, lengthBefore, lengthAfter);
        if (mText == null) {
            return;
        }
        //如果字数不超过用户设置的总字数，就赋值给成员变量mText；
        // 如果字数大于用户设置的总字数，就只保留用户设置的那几位数字，并把光标制动到最后，让用户可以删除；
        if (text.toString().length() <= inputNum) {
            mText = text.toString();
        } else {
            setText(mText);
            setSelection(getText().toString().length());  //光标制动到最后
            //setText(mText)之后键盘会还原，再次把键盘设置为数字键盘；
            setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL);
        }
        if (mCallback != null) {
            mCallback.onSendEvent(0, mText);
        }
    }

    private static final String TAG = "PwdEditText";
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int widthSize = getTotalWidth();

        switch (heightMode) {
            case MeasureSpec.EXACTLY:
                heightSize = MeasureSpec.getSize(heightMeasureSpec);
                break;
            case MeasureSpec.AT_MOST:
                heightSize = widthSize / inputNum;
                break;
        }

//        Log.e(TAG, "onMeasure: ====>>:"+widthSize+"    ");
        setMeasuredDimension(widthSize, heightSize);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //边框画笔
        sidePaint.setAntiAlias(true);//消除锯齿
        sidePaint.setStrokeWidth(3);//设置画笔的宽度
        sidePaint.setStyle(Paint.Style.STROKE);//设置绘制轮廓
        sidePaint.setColor(InputEmpty);
        //背景色画笔
        backPaint.setStyle(Paint.Style.FILL);
        backPaint.setColor(backColor);
        //文字的画笔
        textPaint.setTextSize(18);
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setColor(textColor);
        for (int i = 0; i < inputNum; i++) {
            //区分已输入和未输入的边框颜色
            if (mText.length() >= i) {
                sidePaint.setColor(InputEmpty);
            } else {
                sidePaint.setColor(InputEmpty);
            }

            //RectF的参数(left,  top,  right,  bottom); 画出每个矩形框并设置间距，间距其实是增加左边框距离，缩小上下右边框距离；
            RectF rect = new RectF(i * SingleWidthHeight + spzceX, spzceY, i * SingleWidthHeight + SingleWidthHeight - spzceX, SingleWidthHeight - spzceY); //四个值，分别代表4条线，距离起点位置的线
            canvas.drawRoundRect(rect, 9, 9, backPaint); //绘制背景色
            canvas.drawRoundRect(rect, 9, 9, sidePaint); //绘制边框；
            rectFS.add(rect);
        }
        //画密码圆点
        for (int j = 0; j < mText.length(); j++) {
            canvas.drawCircle(rectFS.get(j).centerX(), rectFS.get(j).centerY(), DisplayUtil.dp2px(getContext(),5), textPaint);
        }
    }

    public int getTotalWidth() {
        return SingleWidthHeight * inputNum ;
    }

}